package com.cty.A_GeneralDataStructures.C_Tree.Tree234;

import com.cty.F_common.DataItem;

import java.util.Scanner;

/**
 * @Auther: cty
 * @Date: 2020/6/16 21:54
 * @Description: 测试234树
 * @version: 1.0
 */
public class Tree234App {
    public static void main(String[] args) {
        Tree234 tree = new Tree234();
        long key;

        tree.insert(new DataItem(50));
        tree.insert(new DataItem(40));
        tree.insert(new DataItem(60));
        tree.insert(new DataItem(30));
        tree.insert(new DataItem(70));

        while(true)
        {
            System.out.print("Enter first letter of show, insert, find or delete: ");
            char choice = getChar();
            switch(choice)
            {
                case 's':
                    tree.displayTree234();
                    break;
                case 'i':
                    System.out.print("Enter key to insert: ");
                    key = getInt();
                    tree.insert(new DataItem(key));
                    break;
                case 'f':
                    System.out.print("Enter key to find: ");
                    key = getInt();
                    DataItem findItem = tree.find(key);
                    if(findItem != null)
                        System.out.println("Found " + findItem.getKey());
                    else
                        System.out.println("Could not find " + key);
                    break;
                case 'd':
                    System.out.print("Enter key to delete: ");
                    key = getInt();
                    DataItem delItem = tree.delete(key);
                    if(delItem != null)
                        System.out.println("Deleted " + delItem.getKey());
                    else
                        System.out.println("Could not delete " + key);
                    break;

                default:
                    System.out.println("Invalid entry");

            }  // end switch
        }  // end while
    }  // end method main

    public static String getString()
    {
        Scanner scan = new Scanner(System.in);
        return scan.nextLine();
    }

    public static char getChar()
    {
        String str = getString();
        return str.charAt(0);
    }

    public static int getInt()
    {
        String str = getString();
        return Integer.parseInt(str);
    }

}  // end Tree234App{}

/**
 * Enter first letter of show, insert, find or delete: s
 * level=0 child=0 50/
 * level=1 child=0 30/40/
 * level=1 child=1 60/70/
 * Enter first letter of show, insert, find or delete: f
 * Enter key to find: 60
 * Found 60
 * Enter first letter of show, insert, find or delete: i
 * Enter key to insert: 45
 * Enter first letter of show, insert, find or delete: i
 * Enter key to insert: 48
 * Enter first letter of show, insert, find or delete: s
 * level=0 child=0 40/50/
 * level=1 child=0 30/
 * level=1 child=1 45/48/
 * level=1 child=2 60/70/
 * Enter first letter of show, insert, find or delete: d
 * Enter key to delete: 45
 * Deleted 45
 * Enter first letter of show, insert, find or delete: s
 * level=0 child=0 40/50/
 * level=1 child=0 30/
 * level=1 child=1 -1/48/
 * level=1 child=2 60/70/
 */
